//
//  MHWebViewData.m
//  CacheHtmlPage
//
//  Created by zsgjs on 2017/2/20.
//  Copyright © 2017年 zsgjs. All rights reserved.
//

#import "MHWebViewData.h"
#import <objc/runtime.h>

@implementation MHWebViewData

+ (NSArray *)propertiesOfSelf
{
    unsigned int count;
    Ivar *list = class_copyIvarList(self, &count);
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count; i++) {
        Ivar ivar = list[i];
        NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSString *propName = [name substringFromIndex:1];
        [arrM addObject:propName];
    }
    
    return [arrM copy];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    NSArray *props = [[self class] propertiesOfSelf];
    
    for (NSString *propName in props) {
        SEL getSel = NSSelectorFromString(propName);
        [aCoder encodeObject:[self performSelector:getSel] forKey:propName];
    }
    
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        
        NSArray *props = [[self class] propertiesOfSelf];
        
        for (NSString *propName in props) {
            NSString *firstCharater = [propName substringToIndex:1].uppercaseString;
            NSString *setPropertyName = [NSString stringWithFormat:@"set%@%@:",firstCharater,[propName substringFromIndex:1]];
            SEL setSel = NSSelectorFromString(setPropertyName);
            [self performSelector:setSel withObject:[coder decodeObjectForKey:propName]];
        }
        
    }
    return self;
}


@end
